package struts.from;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class Struts_xml{
	
	public Struts_xml(){
	}
	
	public static Map<String, XmlBean> Struts_xml(String xmlpath)throws Exception {
		//System.out.println("begin xml......");
		SAXBuilder builder = new SAXBuilder();
		//Document document = builder.build("/bin/struts-config.xml");
		Document document = builder.build(xmlpath);
		Element root	= document.getRootElement();
		Map<String, XmlBean> rmap = new HashMap<String,XmlBean>();
		Element actionroot	= root.getChild("action-mapping");		
		List<Element> actio = actionroot.getChildren();
		String path="";
		for(Element e:actio)
		{
			XmlBean action = new XmlBean();
			String name = e.getAttributeValue("name");
			Element actionform = root.getChild("formbeans");
			List<Element> form = actionform.getChildren();
			for (Element ex:form){
				if(name.equals(ex.getAttributeValue("name")))
				{
					String formclass = ex.getAttributeValue("class");
					action.setFormCalss(formclass);   
					break;
				}
			}			
			path = e.getAttributeValue("path");
			action.setPath(path);			
			String actionClass = e.getAttributeValue("type");
			action.setActionClass(actionClass);
			List<Element> forward = e.getChildren();
			Map<String, String> map = new HashMap<String,String>();
			for(Element x:forward)
			{
				
				String fname = x.getAttributeValue("name");
				String fvalue = x.getAttributeValue("value");
				System.out.println(fname);
				System.out.println(fvalue);
				map.put(fname,fvalue);				
			}
			action.setActionforward(map);
			rmap.put(path, action);
		}
		System.out.println("print xml key");
		Set<String> keyset = rmap.keySet();
		for(Iterator<String> it = keyset.iterator();it.hasNext();)
		{
			String key= it.next();
			System.out.println(key);
			System.out.println(rmap.get(key));
		}

		return rmap;
		
	}
	
	public static Map<String, XmlBean> ddd(String rpath)throws Exception{
		SAXBuilder builder = new SAXBuilder();
		Document document= builder.build(new File(rpath));
		Element root=document.getRootElement();
		Map<String, XmlBean> rMap= new HashMap<String,XmlBean>();
		
		return rMap ;
		
	}





}
